package tr.gov.tubitak.bilgem.esya.certselector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tr.gov.tubitak.uekae.esya.api.common.util.bag.Pair;
import tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.CardType;
import tr.gov.tubitak.uekae.esya.api.smartcard.pkcs11.SmartOp;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ramazan.girgin
 * Date: 11.10.2011
 * Time: 08:26
 * To change this template use File | Settings | File Templates.
 */
public class ReaderListWorker extends SwingWorker<List<ReaderInfo>,Object>
{
    private static Logger logger = LoggerFactory.getLogger(ReaderListWorker.class);
    JTree mTree;
    DefaultMutableTreeNode mRootNode;

    ReaderListWorker(JTree iTree,DefaultMutableTreeNode iRootNode)
    {
        mTree = iTree;
        mRootNode = iRootNode;
    }

    @Override
    protected List<ReaderInfo> doInBackground() throws Exception
    {
        List<ReaderInfo> retReaderList=new ArrayList<ReaderInfo>();
        EReaderListNode eReaderListNode = (EReaderListNode) mRootNode;
        eReaderListNode.setBusy(true);
        try
        {
            ESmartCardManager smartCardManager = ESmartCardManager.getInstance();
            smartCardManager.clear();
            logger.debug("ESmartCardManager'dan kart okuyucu listesi alınacak");
            ArrayList<EBasicSmartCard> basicSmartCards = smartCardManager.getBasicSmartCards();
            logger.debug("ESmartCardManager'dan kart okuyucu listesi alındı.");
            for (EBasicSmartCard basicSmartCard : basicSmartCards) {
                String terminal = basicSmartCard.getTerminal();
                logger.debug(terminal+" card okuyucunun bilgileri ekleniyor.");
                Pair<Long,CardType> slotAndCardType = SmartOp.getSlotAndCardType(terminal);
                basicSmartCard.setSlotNo(slotAndCardType.getObject1());
                ReaderInfo readerInfo = new ReaderInfo(terminal,slotAndCardType.getObject1(),slotAndCardType.getObject2());
                readerInfo.setBasicSmartCard(basicSmartCard);
                retReaderList.add(readerInfo);
                logger.debug(terminal+" card okuyucunun bilgileri eklendi.");
            }
        }
        catch (Exception e)
        {
            //busyComponent.setBusy(false);
            throw e;
        }
        finally
        {
            eReaderListNode.setBusy(false);
        }
        return retReaderList;
    }

       @Override
    public void done()
    {
        List<ReaderInfo> readers;
        try
        {
            readers = get();
            List<DefaultMutableTreeNode> readerNodeList = new ArrayList<DefaultMutableTreeNode>();
            if(readers.isEmpty()){
                return;
            }

            List<ReaderInfo> readerList = new ArrayList<ReaderInfo>();
            for (ReaderInfo reader : readers) {
                readerList.add(reader);
                DefaultMutableTreeNode readerNode = new DefaultMutableTreeNode(reader);
                readerNodeList.add(readerNode);
                mRootNode.add(readerNode);
            }

            TreeUtil.expandTree(mTree, true);
            ReaderCertListWorker readerCertListWorker= new ReaderCertListWorker(mTree,readerList,readerNodeList);
            readerCertListWorker.setmShowOnlyQualified(ismShowOnlyQualified());
            readerCertListWorker.execute();
        }
        catch (InterruptedException ignore)
        {
        }
        catch (java.util.concurrent.ExecutionException e)
        {
        }
    }
       public boolean ismShowOnlyQualified() {
        return mShowOnlyQualified;
    }

    public void setmShowOnlyQualified(boolean iShowOnlyQualified) {
        this.mShowOnlyQualified = iShowOnlyQualified;
    }
    boolean mShowOnlyQualified;
}
